package how2quit.apps.kleague;

import java.util.ArrayList;
import java.util.List;

import how2quit.apps.kleague.views.DragController;
import how2quit.apps.kleague.views.DragLayer;
import how2quit.apps.kleague.views.DragSource;
import how2quit.apps.kleague.views.FormationLinearLayout;
import how2quit.apps.kleague.views.FramedImageView;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class TestActivity extends Activity implements View.OnTouchListener, View.OnClickListener, View.OnLongClickListener {
	
	private static final String TAG = "TestActivity";
	
	private boolean isLongClickStartDrag = true;
	
	ArrayList<FramedImageView> players = new ArrayList<FramedImageView>();
	DragController dragController;
	DragLayer dragLayer;
	FormationLinearLayout formationLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        
        
        dragController = new DragController(this);
        dragLayer = (DragLayer)findViewById(R.id.draglayer);
        formationLinearLayout = (FormationLinearLayout)findViewById(R.id.formation_base);
        dragLayer.setDragController(dragController);
        dragLayer.setFormationLinearLayout(formationLinearLayout);
        
        dragController.setDragListener(dragLayer);
        
        if(formationLinearLayout!=null){
        	int count = countFramedImageView(players, formationLinearLayout);
        	for(int i=0 ; i<count ; i++){
        		FramedImageView view = players.get(i);
        		view.setOnClickListener(this);
        		view.setOnTouchListener(this);
        		view.setOnLongClickListener(this);
        		
        	}
        }
        
        
      
    }
    
    private int countFramedImageView(ArrayList<FramedImageView> list, ViewGroup group) {
    	int count = group.getChildCount();
    	for(int i=0 ; i<count ; i++){
    		View tmp = group.getChildAt(i);
    		
    		if(tmp instanceof ViewGroup)  countFramedImageView(list, (ViewGroup)tmp);
    		else if(tmp instanceof FramedImageView) list.add((FramedImageView)tmp);
    		
    	}
    	return list.size();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_test, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
		Log.i(TAG, "onClick is called\n" + ((FramedImageView)v).getId());
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		/*Log.i(TAG, "onTouch is called\n" + ((FramedImageView)v).getId());
		boolean handledHere = false;
		
		final int action = event.getAction();
		if(action == MotionEvent.ACTION_DOWN){
			handledHere = startDrag(v);
		}*/
		return false;
	}
	
	private boolean startDrag(View v){
		DragSource dragSource = (DragSource)v;
		dragController.startDrag(v, dragSource, dragSource, DragController.DRAG_ACTION_MOVE);
		
		return true;
	}

	@Override
	public boolean onLongClick(View v) {
		Log.i(TAG, "Long clicked");
		if(isLongClickStartDrag){
			if(!v.isInTouchMode()){
				return false;
			}
			return startDrag(v);
		}
		return false;
	}
}
